package wqh.controller.json;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import wqh.domain.Device;
import wqh.domain.GeoBuilding;
import wqh.domain.Monitor;
import wqh.domain.Sensor;
import wqh.pojo.*;
import wqh.service.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by devts on 2016/9/26.
 */
@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/xkMonitor")
public class MonitorJSON {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MonitorService monitorService;

    @Autowired
    private GeoBuildingService geoBuildingService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private SensorService sensorService;

    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestParam(value = "geo_level_1_id",required = true) int geo_level_1_id,
                      @RequestParam(value = "geo_level_2_id",required = true) int geo_level_2_id,
                      @RequestParam(value = "geo_level_3_id",required = true) int geo_level_3_id,
                      @RequestParam(value = "device_id",required = true) int device_id,
                      @RequestParam(value = "sensor_id",required = true) int sensor_id,
                      @RequestParam(value = "name",required = true) String name,
                      @RequestParam(value = "pos_x",required = false,defaultValue = "0") String pos_x,
                      @RequestParam(value = "pos_y",required = false,defaultValue = "0") String pos_y,
                      @RequestParam(value = "code",required = false,defaultValue = "0") String description)
    {
        //注意，这里暂用描述信息的字段存取报警回路号。
        try {
            Monitor monitor = new Monitor(geo_level_1_id,geo_level_2_id,geo_level_3_id,device_id,sensor_id,name,pos_x,pos_y,description);
            monitorService.insertRecord(monitor);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.toString());
            return MapRet.SQL_INSERT_ERROR();
        }


        return MapRet.SQL_INSERT_SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@RequestParam(value = "id",required = true) int id,
                         @RequestParam(value = "geo_level_1_id",required = true) int geo_level_1_id,
                         @RequestParam(value = "geo_level_2_id",required = true) int geo_level_2_id,
                         @RequestParam(value = "geo_level_3_id",required = true) int geo_level_3_id,
                         @RequestParam(value = "device_id",required = true) int device_id,
                         @RequestParam(value = "sensor_id",required = true) int sensor_id,
                         @RequestParam(value = "name",required = true) String name,
                         @RequestParam(value = "pos_x",required = false,defaultValue = "0") String pos_x,
                         @RequestParam(value = "pos_y",required = false,defaultValue = "0") String pos_y,
                         @RequestParam(value = "code",required = false,defaultValue = "0") String description)
    {
        //注意，这里暂用描述信息的字段存取报警回路号。
        try {
            Monitor monitor = new Monitor(id,geo_level_1_id,geo_level_2_id,geo_level_3_id,device_id,sensor_id,name,pos_x,pos_y,description);
            monitorService.updateRecord(monitor);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.toString());
            return MapRet.SQL_UPDATE_ERROR();
        }

        return MapRet.SQL_UPDATE_SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Object delete(@RequestParam(value = "id",required = true) int id)
    {
        try {
            monitorService.deleteRecord(id);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.toString());
            return MapRet.SQL_DELETE_ERROR();
        }


        return MapRet.SQL_DELETE_SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/updateCode",method = RequestMethod.POST)
    public Object updateCode(@RequestParam(value = "id",required = true) int id,
                             @RequestParam(value = "code",required = true) String code)
    {
        //注意，这里暂用描述信息的字段存取报警回路号。
        try {
            Monitor m = monitorService.findAllById(id);
            monitorService.updateDescriptionById(code,id);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.toString());
            return MapRet.SQL_UPDATE_ERROR();
        }


        return MapRet.SQL_UPDATE_SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/updateName",method = RequestMethod.POST)
    public Object updateName(@RequestParam(value = "id",required = true) int id,
                             @RequestParam(value = "name",required = true) String name)
    {
        //注意，这里暂用描述信息的字段存取报警回路号。
        try {
            monitorService.updateNameById(name,id);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.toString());
            return MapRet.SQL_UPDATE_ERROR();
        }
        return MapRet.SQL_UPDATE_SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/updatePos",method = RequestMethod.POST)
    public Object updatePos(@RequestParam(value = "id",required = true) int id,
                           @RequestParam(value = "x",required = true) String pos_x,
                           @RequestParam(value = "y",required = true) String pos_y)
    {
        //注意，这里暂用描述信息的字段存取报警回路号。
        try {
            monitorService.updatePosById(pos_x,pos_y,id);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.toString());
            return MapRet.SQL_UPDATE_ERROR();
        }


        return MapRet.SQL_UPDATE_SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getAll()
    {
        List<Monitor> monitorList = monitorService.findAllRecord();
        List<MonitorJsonPojo> monitorJsonPojoList = new ArrayList<MonitorJsonPojo>();
/*        for(Monitor monitor : monitorList)
        {
            String geo1 = geoBuildingService.findNameById(monitor.getGeo_level_1_id());
            String geo2 = geoBuildingService.findNameById(monitor.getGeo_level_2_id());
            String geo3 = geoBuildingService.findNameById(monitor.getGeo_level_3_id());
            Device device = deviceService.findAllByID(monitor.getDevice_id());
            String device_name = device.getName();
            String device_ip = IPConver.int2Ip(device.getIp());
            String sensor_name = sensorService.findNameByID(monitor.getSensor_id());
            MonitorJsonPojo m = new MonitorJsonPojo(monitor.getId(),
                    monitor.getGeo_level_1_id(),
                    geo1,
                    monitor.getGeo_level_2_id(),
                    geo2,
                    monitor.getGeo_level_3_id(),
                    geo3,
                    monitor.getDevice_id(),
                    device_name,
                    device_ip,
                    monitor.getSensor_id(),
                    sensor_name,
                    monitor.getName(),
                    monitor.getPos_x(),
                    monitor.getPos_y(),
                    monitor.getDescription());
            monitorJsonPojoList.add(m);
        }*/
        List<GeoBuilding> geoBuildingList = geoBuildingService.findAllRecord();
        List<Device> deviceList = deviceService.findAllRecord();
        List<Sensor> sensorList = sensorService.findAllRecord();
        for(Monitor monitor : monitorList){
            String geo1 = null;
            for(GeoBuilding geoBuilding : geoBuildingList){
                if(geoBuilding.getId().equals(monitor.getGeo_level_1_id())){
                    geo1 = geoBuilding.getName();
                    break;
                }
            }

            String geo2= null;
            for(GeoBuilding geoBuilding : geoBuildingList){
                if(geoBuilding.getId().equals(monitor.getGeo_level_2_id())){
                    geo2 = geoBuilding.getName();
                    break;
                }
            }

            String geo3 = null;
            for(GeoBuilding geoBuilding : geoBuildingList){
                if(geoBuilding.getId().equals(monitor.getGeo_level_3_id())){
                    geo3 = geoBuilding.getName();
                    break;
                }
            }

            Device device = null;
            for(Device d : deviceList){
                if(d.getId().equals(monitor.getDevice_id())){
                    device = d;
                    break;
                }
            }

            String device_name = device.getName();
            String device_ip = IPConver.int2Ip(device.getIp());

            String sensor_name = null;
            for(Sensor sensor : sensorList){
                if(sensor.getId().equals(monitor.getSensor_id())){
                    sensor_name = sensor.getName();
                    break;
                }
            }

            MonitorJsonPojo m = new MonitorJsonPojo(monitor.getId(),
                    monitor.getGeo_level_1_id(),
                    geo1,
                    monitor.getGeo_level_2_id(),
                    geo2,
                    monitor.getGeo_level_3_id(),
                    geo3,
                    monitor.getDevice_id(),
                    device_name,
                    device_ip,
                    monitor.getSensor_id(),
                    sensor_name,
                    monitor.getName(),
                    monitor.getPos_x(),
                    monitor.getPos_y(),
                    monitor.getDescription());
            monitorJsonPojoList.add(m);

        }

        return MapRet.DATA(monitorJsonPojoList);
    }

    @ResponseBody
    @RequestMapping(value = "/getAllNoPos",method = RequestMethod.GET)
    public Object getAllNoPos()
    {
        List<Monitor> monitorList = monitorService.findAllRecord();
        List<MonitorJsonPojo> monitorJsonPojoList = new ArrayList<MonitorJsonPojo>();
        for(Monitor monitor : monitorList)
        {
            if(monitor.getPos_x() == null || monitor.getPos_y() == null || monitor.getPos_x().equals("0") || monitor.getPos_y().equals("0"))
            {
                String geo1 = geoBuildingService.findNameById(monitor.getGeo_level_1_id());
                String geo2 = geoBuildingService.findNameById(monitor.getGeo_level_2_id());
                String geo3 = geoBuildingService.findNameById(monitor.getGeo_level_3_id());
                Device device = deviceService.findAllByID(monitor.getDevice_id());
                String device_name = device.getName();
                String device_ip = IPConver.int2Ip(device.getIp());
                String sensor_name = sensorService.findNameByID(monitor.getSensor_id());
                MonitorJsonPojo m = new MonitorJsonPojo(monitor.getId(),
                        monitor.getGeo_level_1_id(),
                        geo1,
                        monitor.getGeo_level_2_id(),
                        geo2,
                        monitor.getGeo_level_3_id(),
                        geo3,
                        monitor.getDevice_id(),
                        device_name,
                        device_ip,
                        monitor.getSensor_id(),
                        sensor_name,
                        monitor.getName(),
                        monitor.getPos_x(),
                        monitor.getPos_y(),
                        monitor.getDescription());
                monitorJsonPojoList.add(m);
            }
        }
        return MapRet.DATA(monitorJsonPojoList);
    }

    @ResponseBody
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public Object get(@RequestParam(value = "id",required = true) int id)
    {
        Monitor monitor = monitorService.findAllById(id);
        Integer sensor_level_1 = null;
        Integer sensor_level_2 = null;
        Integer sensor_level_3 = null;
        Sensor sensor3 =sensorService.findAllByID(monitor.getSensor_id());
        if(sensor3.getLevel() == 3)
        {
            sensor_level_3 = sensor3.getId();
            Sensor sensor2 =sensorService.findAllByID(sensor3.getParent_id());
            if(sensor2.getLevel() == 2)
            {
                sensor_level_2 = sensor2.getId();
                Sensor sensor1 =sensorService.findAllByID(sensor2.getParent_id());
                if(sensor1.getLevel() == 1)
                {
                    sensor_level_1 = sensor1.getId();
                }
            }
        }
        MonitorGetOneJson m = new MonitorGetOneJson(monitor.getId(),
                monitor.getGeo_level_1_id(),
                monitor.getGeo_level_2_id(),
                monitor.getGeo_level_3_id(),
                monitor.getDevice_id(),
                sensor_level_1,
                sensor_level_2,
                sensor_level_3,
                monitor.getName(),
                monitor.getPos_x(),
                monitor.getPos_y(),
                monitor.getDescription());
        return MapRet.DATA(m);
    }

    @ResponseBody
    @RequestMapping(value = "/getFloor",method = RequestMethod.GET)
    public Object getFloor(@RequestParam(value = "id",required = true) int id)
    {
        List<Monitor> monitorList = monitorService.findAllByGeoLevel3(id);
        List<MonitorJsonPojo> monitorJsonPojoList = new ArrayList<MonitorJsonPojo>();
        for(Monitor monitor : monitorList)
        {
            String geo1 = geoBuildingService.findNameById(monitor.getGeo_level_1_id());
            String geo2 = geoBuildingService.findNameById(monitor.getGeo_level_2_id());
            String geo3 = geoBuildingService.findNameById(monitor.getGeo_level_3_id());
            Device device = deviceService.findAllByID(monitor.getDevice_id());
            String device_name = device.getName();
            String device_ip = IPConver.int2Ip(device.getIp());
            String sensor_name = sensorService.findNameByID(monitor.getSensor_id());
            MonitorJsonPojo m = new MonitorJsonPojo(monitor.getId(),
                    monitor.getGeo_level_1_id(),
                    geo1,
                    monitor.getGeo_level_2_id(),
                    geo2,
                    monitor.getGeo_level_3_id(),
                    geo3,
                    monitor.getDevice_id(),
                    device_name,
                    device_ip,
                    monitor.getSensor_id(),
                    sensor_name,
                    monitor.getName(),
                    monitor.getPos_x(),
                    monitor.getPos_y(),
                    monitor.getDescription());
            monitorJsonPojoList.add(m);
        }
        return MapRet.DATA(monitorJsonPojoList);
    }

}
